package cn.tsmchina.one.util;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

/**
 * ASE 加密解密
 * @author YUE
 * @date 2013-4-15 上午9:57:30
 */
public class AESUtil
{

	private static final String AES = "AES";
	private static final String KEY = "wgahdtekiadhncga";

	/**
	 * 加密
	 * 
	 * @author YUE
	 * @date 2013-4-12 下午4:50:16
	 * @param value
	 * @return
	 */
	public static String encrypt(String value)
	{
		try
		{
			SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes("utf-8"), AES);
			Cipher cipher = Cipher.getInstance(AES);
			cipher.init(Cipher.ENCRYPT_MODE, keySpec);
			return Base64.encode(cipher.doFinal(value.getBytes("utf-8")));
		}
		catch (Exception e)
		{
		}
		return null;
	}

	/**
	 * 解密
	 * 
	 * @author YUE
	 * @date 2013-4-12 下午4:51:37
	 * @param value
	 * @return
	 */
	public static String decrypt(String value)
	{
		try
		{
			SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes("utf-8"), AES);
			Cipher cipher = Cipher.getInstance(AES);
			cipher.init(Cipher.DECRYPT_MODE, keySpec);
			byte[] bytes = cipher.doFinal(Base64.decode(value));
			return new String(bytes,"utf-8");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
}
